package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.mapper.*;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BarCodeUtils;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.service.IWmsDeliveryPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-09
 */
@Service
public class WmsDeliveryPlanServiceImpl implements IWmsDeliveryPlanService
{
    @Autowired
    private WmsDeliveryPlanMapper wmsDeliveryPlanMapper;

    @Autowired
    private WmsDeliveryRecordMapper wmsDeliveryRecordMapper;

    @Autowired
    private WmsTransferMapper wmsTransferMapper;

    @Autowired
    private WmsTransferRecordMapper wmsTransferRecordMapper;

    @Autowired
    private WmsDeliveryPlanDetailMapper wmsDeliveryPlanDetailMapper;

    /**
     * 查询出库计划
     *
     * @param id 出库计划主键
     * @return 出库计划
     */
    @Override
    public WmsDeliveryPlan selectWmsDeliveryPlanById(Long id)
    {
        WmsDeliveryPlan wmsDeliveryPlan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(id);
        if (wmsDeliveryPlan != null) {
            wmsDeliveryPlan.setBarCode(BarCodeUtils.barCodeImageBase64(wmsDeliveryPlan.getDeliveryNo()));
        }
        return wmsDeliveryPlan;
    }

    /**
     * 查询出库计划列表
     *
     * @param wmsDeliveryPlan 出库计划a
     * @return 出库计划
     */
    @Override
    public List<WmsDeliveryPlan> selectWmsDeliveryPlanList(WmsDeliveryPlan wmsDeliveryPlan)
    {
        return wmsDeliveryPlanMapper.selectWmsDeliveryPlanList(wmsDeliveryPlan);
    }

    /**
     * 新增出库计划
     *
     * @param wmsDeliveryPlan 出库计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWmsDeliveryPlan(WmsDeliveryPlan wmsDeliveryPlan)
    {
        wmsDeliveryPlan.setDeliveryNo(DateUtils.dateTimeNow(DateUtils.YYYYMMDD));
        wmsDeliveryPlan.setCreateTime(DateUtils.getNowDate());
        wmsDeliveryPlan.setStatus("wait");
        int i = wmsDeliveryPlanMapper.insertWmsDeliveryPlan(wmsDeliveryPlan);
        WmsDeliveryPlan deliveryPlan = selectWmsDeliveryPlanById(wmsDeliveryPlan.getId());
        String deliveryNo = "CK" + deliveryPlan.getDeliveryNo();
        // 创建出库单
        WmsDeliveryRecord wmsDeliveryRecord = new WmsDeliveryRecord();
        BeanUtils.copyProperties(wmsDeliveryPlan, wmsDeliveryRecord, "id", "deliveryNo");
        wmsDeliveryRecord.setDeliveryNo(deliveryNo);
        wmsDeliveryRecord.setPlanId(wmsDeliveryPlan.getId());
        wmsDeliveryRecordMapper.insertWmsDeliveryRecord(wmsDeliveryRecord);

        if ("transfer".equalsIgnoreCase(wmsDeliveryPlan.getDeliveryType()) && StringUtils.isNotBlank(wmsDeliveryPlan.getRelateNo())) {
            WmsTransfer wmsTransfer = wmsTransferMapper.selectWmsTransferBySn(wmsDeliveryPlan.getRelateNo());
            if (wmsTransfer == null) {
                throw new ServiceException("没有找到关联单据信息");
            }
            Long transferId = wmsTransfer.getId();
            WmsTransferRecord query = new WmsTransferRecord();
            query.setTransferId(transferId);
            List<WmsTransferRecord> transferRecordList = wmsTransferRecordMapper.selectWmsTransferRecordList(query);
            // 同步调拨单数据到出库计划明细
            if (ObjectUtils.isNotEmpty(transferRecordList)) {
                for (WmsTransferRecord wmsTransferRecord : transferRecordList) {
                    WmsDeliveryPlanDetail deliveryPlanDetail = new WmsDeliveryPlanDetail();
                    deliveryPlanDetail.setDeliveryNo(deliveryPlan.getDeliveryNo());
                    deliveryPlanDetail.setCargoId(wmsTransferRecord.getCargoId());
                    deliveryPlanDetail.setCargoName(wmsTransferRecord.getCargoName());
                    deliveryPlanDetail.setCargoSn(wmsTransferRecord.getCargoSn());
                    deliveryPlanDetail.setCargoUnit(wmsTransferRecord.getCargoUnit());
                    deliveryPlanDetail.setCargoSpec(wmsTransferRecord.getCargoSpec());
                    deliveryPlanDetail.setPlannedNum(wmsTransferRecord.getNum());
//                    deliveryPlanDetail.setSupplierSn();
//                    deliveryPlanDetail.setLotNo();
                    wmsDeliveryPlanDetailMapper.insertWmsDeliveryPlanDetail(deliveryPlanDetail);
                }
            }
        }
        return i;
    }

    /**
     * 修改出库计划
     *
     * @param wmsDeliveryPlan 出库计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWmsDeliveryPlan(WmsDeliveryPlan wmsDeliveryPlan)
    {
        wmsDeliveryPlan.setDeliveryNo(null);
        wmsDeliveryPlan.setUpdateTime(DateUtils.getNowDate());
        wmsDeliveryPlan.setStatus(null);
        int i = wmsDeliveryPlanMapper.updateWmsDeliveryPlan(wmsDeliveryPlan);
        WmsDeliveryPlan deliveryPlan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(wmsDeliveryPlan.getId());
        // 更新出库单
//        String deliveryNo = "CK" + deliveryPlan.getDeliveryNo();
//        WmsDeliveryRecord old = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByDeliveryNo(deliveryNo);
        WmsDeliveryRecord old = wmsDeliveryRecordMapper.selectWmsDeliveryRecordByPlanId(wmsDeliveryPlan.getId());
        BeanUtils.copyProperties(deliveryPlan, old, "id", "deliveryNo", "createTime", "createBy", "planId");
        wmsDeliveryRecordMapper.deleteWmsDeliveryRecordById(old.getId());
        wmsDeliveryRecordMapper.insertWmsDeliveryRecord(old);
        return i;
    }

    /**
     * 取消出库计划
     *
     * @param wmsDeliveryPlan
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancel(WmsDeliveryPlan wmsDeliveryPlan) {
        WmsDeliveryPlan deliveryPlan = wmsDeliveryPlanMapper.selectWmsDeliveryPlanById(wmsDeliveryPlan.getId());
        String status = deliveryPlan.getStatus();
        if ("all".equalsIgnoreCase(status)) {
            throw new ServiceException("已出库,不能取消");
        }
        if ("partial".equalsIgnoreCase(status)) {
            throw new ServiceException("已部分出库,不能取消");
        }
        WmsDeliveryPlan plan = new WmsDeliveryPlan();
        plan.setId(deliveryPlan.getId());
        plan.setStatus("canceled");
        // 删除出库单
//        String deliveryNo = "CK" + deliveryPlan.getDeliveryNo();
//        wmsDeliveryRecordMapper.deleteWmsDeliveryRecordByDeliveryNo(deliveryNo);
        wmsDeliveryRecordMapper.deleteWmsDeliveryRecordByPlanId(plan.getId());
        return wmsDeliveryPlanMapper.updateWmsDeliveryPlan(plan);
    }

    /**
     * 批量删除出库计划
     *
     * @param ids 需要删除的出库计划主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeliveryPlanByIds(Long[] ids)
    {
        return wmsDeliveryPlanMapper.deleteWmsDeliveryPlanByIds(ids);
    }

    /**
     * 删除出库计划信息
     *
     * @param id 出库计划主键
     * @return 结果
     */
    @Override
    public int deleteWmsDeliveryPlanById(Long id)
    {
        return wmsDeliveryPlanMapper.deleteWmsDeliveryPlanById(id);
    }
}
